<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: EntryService.cfc 1177 2007-06-25 18:20:41Z pfarrell $

Notes:
--->
<cfcomponent
	displayname="EntryService"
	output="false"
	hint="EntryService CFC for MachBlog">
		
	<!---
	PROPERTIES
	--->
	<cfproperty name="entryGateway" type="EntryGateway" required="true" />
	<cfproperty name="entryDAO" type="EntryDAO" required="true" />
	<cfproperty name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
	<cfproperty name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
	<cfproperty name="udfs" type="org.machblog.utils.udfs" required="true" />

	<!---
	INITIALIZATION / CONFIGURATION
	--->	
	<cffunction name="init" access="public" output="false" returntype="EntryService"
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>

	<cffunction name="setEntryGateway" access="public" output="false" returntype="void">
		<cfargument name="entryGateway" type="EntryGateway" required="true" />
		<cfset variables.entryGateway = arguments.entryGateway />
	</cffunction>	
	<cffunction name="getEntryGateway" access="public" output="false" returntype="EntryGateway">
		<cfreturn variables.entryGateway />
	</cffunction>
	
	<cffunction name="setEntryDAO" access="public" output="false" returntype="void">
		<cfargument name="entryDAO" type="EntryDAO" required="true" />
		<cfset variables.entryDAO = arguments.entryDAO />
	</cffunction>	
	<cffunction name="getEntryDAO" access="public" output="false" returntype="EntryDAO">
		<cfreturn variables.entryDAO />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>
	<cffunction name="getUserFacade" access="public" output="false" returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<cffunction name="setNotificationService" access="public" output="false" returntype="void">
		<cfargument name="notificationService" type="org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>
	<cffunction name="getNotificationService" access="public" output="false" returntype="org.machblog.notification.NotificationService">
		<cfreturn variables.notificationService />
	</cffunction>
	
	<cffunction name="setUdfs" access="public" output="false" returntype="void">
		<cfargument name="udfs" type="org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUdfs" access="public" output="false" returntype="org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>
	
	<cffunction name="getEntryBean" access="public" output="false" returntype="Entry">
		<cfreturn createObject("component", "Entry").init() />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - GATEWAY RELATED
	--->
	<cffunction name="getEntriesForHomePage" access="public" output="false" returntype="query" 
			hint="Gets entries for the home page">
		<cfargument name="numDays" type="numeric" required="true" />
		<cfreturn getEntryGateway().getEntries(getBy = "homePage", numDays = arguments.numDays) />
	</cffunction>
	
	<cffunction name="getEntriesByCategory" access="public" output="false" returntype="query" 
			hint="Gets entries in a particular category">
		<cfargument name="categoryId" type="uuid" required="true" />
		<cfargument name="numToReturn" type="numeric" required="false" default="-1" />
		<cfreturn getEntryGateway().getEntries(getBy = "category", categoryId = arguments.categoryId, 
												numToReturn = arguments.numToReturn) />
	</cffunction>
	
	<cffunction name="getEntriesByMonth" access="public" output="false" returntype="query" 
			hint="Gets entries for a particular month and year">
		<cfargument name="month" type="numeric" required="true" />
		<cfargument name="year" type="numeric" required="true" />
		<cfargument name="numToReturn" type="numeric" required="false" default="-1" />
		<cfreturn getEntryGateway().getEntries(getBy = "month", month = arguments.month, year = arguments.year, 
												numToReturn = arguments.numToReturn) />
	</cffunction>
	
	<cffunction name="getEntriesByDate" access="public" output="false" returntype="query" 
			hint="Gets entries for a particular date">
		<cfargument name="date" type="date" required="true" />
		<cfreturn getEntryGateway().getEntriesByDate(arguments.date) />
	</cffunction>
	
	<cffunction name="getAllEntries" access="public" output="false" returntype="query" 
			hint="Returns a query object containing all entries (active and inactive).">
		<cfargument name="searchTerms" type="string" required="false" default="" />
		<cfargument name="getActiveOnly" type="boolean" required="false" default="false" />
		<cfreturn getEntryGateway().getEntries(searchTerms = arguments.searchTerms, 
												getActiveOnly = arguments.getActiveOnly) />
	</cffunction>
	
	<cffunction name="getEntries" access="public" output="false" returntype="query" 
			hint="Returns a query object containing active entries, optionally a specific number of entries or currently posted.">
		<cfargument name="numToReturn" type="numeric" required="false" default="-1" />
		<cfargument name="currentPosts" type="boolean" required="false" default="false" />
		<cfreturn getEntryGateway().getEntries(numToReturn = arguments.numToReturn, currentPosts = arguments.currentPosts) />
	</cffunction>
	
	<cffunction name="searchEntries" access="public" output="false" returntype="query" 
			hint="Returns a query object containing entries based on the search terms passed in">
		<cfargument name="searchTerms" type="string" required="true" />
		<cfreturn getEntryGateway().getEntries(getBy = "all", searchTerms = arguments.searchTerms) />
	</cffunction>
	
	<cffunction name="getEntryTitleById" access="public" output="false" returntype="string" 
			hint="Returns an entry title based on the ID passed in">
		<cfargument name="entryId" type="uuid" required="true" />
		
		<cfreturn getEntryGateway().getEntryTitleById(arguments.entryId) />
	</cffunction>
	
	<cffunction name="getEntryCount" access="public" output="false" returntype="numeric" 
			hint="Returns the total number of entries in the blog">
		<cfreturn getEntryGateway().getEntryCount() />
	</cffunction>
	
	<cffunction name="getCurrentUserEntryCount" access="public" output="false" returntype="numeric" 
			hint="Gets the entry count for the currently logged-in user">
		<cfreturn getEntryGateway().getEntryCount(getUserFacade().getUser().getUserId()) />
	</cffunction>
	
	<cffunction name="getArchiveList" access="public" output="false" returntype="array" 
			hint="Returns an array containing month start epochs and the number of entries for the month.">
		<cfargument name="numMonths" type="numeric" required="true" hint="The number of months to return" />
		
		<cfreturn getEntryGateway().getArchiveList(arguments.numMonths) />
	</cffunction>
	
	<cffunction name="incrementViews" access="public" output="false" returntype="void" 
			hint="Increments the number of times the entry has been viewed">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfset getEntryGateway().incrementViews(arguments.entryId) />
	</cffunction>
	
	<cffunction name="getUnnotifiedEntries" access="public" output="false" returntype="query" 
			hint="Returns a query object containing the entries for which subscription notifications have not been sent">
		<cfreturn getEntryGateway().getUnnotifiedEntries() />
	</cffunction>
	
	<cffunction name="notifySubscribers" access="public" output="false" returntype="void" 
			hint="Notifies blog subscribers of a new or updated entry">
		<cfargument name="entryId" type="uuid" required="true" />
		<cfargument name="entryTitle" type="string" required="true" />
		<cfargument name="subscribers" type="query" required="true" />
		<cfargument name="emailSubject" type="string" required="true" />
		<cfargument name="fromAddress" type="string" required="true" />
		<cfargument name="blogTitle" type="string" required="true" />
		<cfargument name="blogUrl" type="string" required="true" />
		<cfargument name="unsubscribeMessage" type="string" required="true" />
		<cfargument name="eventParameter" type="string" required="true" />
		
		<cfset var entryUrl = "#arguments.blogUrl#/index.cfm?#arguments.eventParameter#=showEntry&entryId=#arguments.entryId#" />
		<cfset var theEmailSubject = "#arguments.emailSubject# #arguments.blogTitle#" />
		<cfset var unsubscribeUrl = "#arguments.blogUrl#/index.cfm?#arguments.eventParameter#=processSubscribeForm&action=unsubscribe&email=" />
		
		<cfset getNotificationService().emailEntryNotification(arguments.fromAddress, arguments.subscribers, theEmailSubject, 
																arguments.entryTitle, entryUrl, arguments.unsubscribeMessage, 
																unsubscribeUrl) />
		
		<cfset getEntryGateway().updateSubscriberEmailFlag(arguments.entryId) />
	</cffunction>

	<!---
	PUBLIC FUNCTIONS - DAO RELATED
	--->
	<cffunction name="getEntryById" access="public" output="false" 
			returntype="Entry" hint="Gets a single blog entry">
		<cfargument name="entryId" type="string" required="true" />
		
		<cfset var entry = getEntryBean().init(arguments.entryId) />
		<cfset getEntryDAO().read(entry) />
		
		<cfreturn entry />
	</cffunction>
	
	<cffunction name="createEntry" access="public" output="false" returntype="void" hint="Creates an entry">
		<cfargument name="entry" type="Entry" required="true" />
		
		<!--- strip any MS Word 'smart quotes' and other smart characters --->
		<cfset arguments.entry.setBody(getUdfs().stripSmartQuotes(arguments.entry.getBody())) />
		<cfset arguments.entry.setMoreBody(getUdfs().stripSmartQuotes(arguments.entry.getMoreBody())) />
		
		<cfset getEntryDAO().create(arguments.entry) />
	</cffunction>
	
	<cffunction name="updateEntry" access="public" output="false" returntype="void" hint="Updates an entry">
		<cfargument name="entry" type="Entry" required="true" />

		<!--- strip any MS Word 'smart quotes' and other smart characters --->
		<cfset arguments.entry.setBody(getUdfs().stripSmartQuotes(arguments.entry.getBody())) />
		<cfset arguments.entry.setMoreBody(getUdfs().stripSmartQuotes(arguments.entry.getMoreBody())) />
		
		<cfset getEntryDAO().update(arguments.entry) />
	</cffunction>
	
	<cffunction name="deleteEntry" access="public" output="false" returntype="void" hint="Deletes an entry">
		<cfargument name="entry" type="Entry" required="true" />
		<cfset getEntryDAO().delete(arguments.entry) />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - UTILITY
	--->
	<cffunction name="deleteFile" access="public" output="false" returntype="void" hint="Deletes a file">
		<cfargument name="theFile" type="string" required="true" />
		
		<cffile action="delete" file="#arguments.theFile#" />
	</cffunction>
</cfcomponent>
